//
//  TEMainViewController.m
//  TintExample
//
//  Created by Kevin Wu on 8/31/12.
//  Copyright (c) 2012 Telligenty. All rights reserved.
//

#import "TEMainViewController.h"


// DB
#import "TEDatabaseViewController.h"
#import "TEDatabaseObjectViewController.h"

// Network
#import "TEHTTPRequestViewController.h"
#import "TENAIManagerViewController.h"
#import "TEReachabilityViewController.h"

// Util
#import "TECacheViewController.h"
#import "TEURLImageLoaderViewController.h"
#import "TEWebServiceViewController.h"
#import "TETestViewController.h"

// XML
#import "TEXMLViewController.h"
#import "TEXMLObjectViewController.h"


@implementation TEMainViewController


#pragma mark - Memory

- (id)init
{
    self = [super init];
    if ( self ) {
        
        self.navigationItem.title = NSLocalizedString(@"Examples", @"");
        
        
        _list = TECreateList();
        
        id group = nil;
        NSArray *examples = nil;
        
        
        group = TEAddGroup(_list, @"Additions");
        examples = TEExamplesOfGroup(group);
        
        
        group = TEAddGroup(_list, @"Basic");
        examples = TEExamplesOfGroup(group);
        
        
        group = TEAddGroup(_list, @"Core");
        examples = TEExamplesOfGroup(group);
        
        
        group = TEAddGroup(_list, @"DB");
        examples = TEExamplesOfGroup(group);
        TEAddExample(examples, @"Database", [TEDatabaseViewController class]);
        TEAddExample(examples, @"Database Object", [TEDatabaseObjectViewController class]);
        
        
        group = TEAddGroup(_list, @"External");
        examples = TEExamplesOfGroup(group);
        
        
        group = TEAddGroup(_list, @"Network");
        examples = TEExamplesOfGroup(group);
        TEAddExample(examples, @"HTTP Request", [TEHTTPRequestViewController class]);
        TEAddExample(examples, @"NAI Manager", [TENAIManagerViewController class]);
        TEAddExample(examples, @"Reachability", [TEReachabilityViewController class]);
        
        
        group = TEAddGroup(_list, @"Util");
        examples = TEExamplesOfGroup(group);
        TEAddExample(examples, @"Cache", [TECacheViewController class]);
        TEAddExample(examples, @"URL Image Loader", [TEURLImageLoaderViewController class]);
        TEAddExample(examples, @"Web Service", [TEWebServiceViewController class]);
        TEAddExample(examples, @"Test", [TETestViewController class]);
        
        
        group = TEAddGroup(_list, @"XML");
        examples = TEExamplesOfGroup(group);
        TEAddExample(examples, @"XML", [TEXMLViewController class]);
        TEAddExample(examples, @"XML Object", [TEXMLObjectViewController class]);
    }
    return self;
}

- (void)dealloc
{
    TTRelease(_list);
    TTRelease(_tableView);
    [super dealloc];
}



#pragma mark - UIViewController

- (void)loadView {
    [super loadView];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [_tableView setFrameIfNeeded:self.view.bounds];
}



#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_list count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    id group = [_list objectAtIndex:section];
    NSArray *examples = TEExamplesOfGroup(group);
    return [examples count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    id group = [_list objectAtIndex:section];
    return TENameOfGroup(group);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithClass:[UITableViewCell class]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    id group = [_list objectAtIndex:indexPath.section];
    NSArray *examples = TEExamplesOfGroup(group);
    id example = [examples objectAtIndex:indexPath.row];
    cell.textLabel.text = TENameOfExample(example);
    
    return cell;
}


#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    id group = [_list objectAtIndex:indexPath.section];
    NSArray *examples = TEExamplesOfGroup(group);
    id example = [examples objectAtIndex:indexPath.row];
    Class controller = TEControllerOfExample(example);
    UIViewController *vc = [[controller alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    TTRelease(vc);
}


@end
